package org.dromara.work.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.SysXzqhBo;
import org.dromara.system.domain.vo.SysXzqhVo;
import org.dromara.system.service.ISysXzqhService;
import org.dromara.work.domain.bo.DmBaseComplexSaveBo;
import org.dromara.work.domain.bo.DmBaseQueryBo;
import org.dromara.work.domain.vo.*;
import org.dromara.work.service.IDmBaseService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.service.ITempDmBaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 标准地名
 *
 * @author Chen
 * @date 2024-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/temp/dm")
public class DmTempController extends BaseController {


    @Resource
    private final ITempDmBaseService iDmService;

    @Resource
    private final ISysXzqhService xzqhService;
    /**
     * 地名基础信息查询
     */
    @SaCheckPermission("temp:dm:list")
    @GetMapping(value = "/pageQuery")
    public TableDataInfo<DmBaseBriefVo> page(@Validated(QueryGroup.class) DmBaseQueryBo bo, PageQuery pageQuery) {
        //用户只能查询自己所在行政区的数据，与部门名称关联。
        if(StringUtils.isBlank(bo.getXzqh())){
            String name=LoginHelper.getDeptName();
            SysXzqhBo sysXzqhBo =new SysXzqhBo();
            List<SysXzqhVo> list= new ArrayList<>();
            String xzq="";
            if(StringUtils.isNotBlank(name)){
                sysXzqhBo.setName(name);
                list=xzqhService.queryList(sysXzqhBo);
            }
            if(!list.isEmpty()){
                xzq=list.get(0).getId()+"";
            }
            bo.setXzqh(xzq);
        }
        return iDmService.customPageList(bo, pageQuery);
    }


    /**
     * 获取地名属性信息详细信息
     *
     * @param id 业务id
     */
    @SaCheckPermission("temp:dm:list")
    @GetMapping("/{id}")
    public R<DmBaseComplexVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable String id) {
        return R.ok(iDmService.queryBySubmitId(id));
    }

    /**
     * 新增地名属性信息
     */
    @SaCheckPermission("temp:dm:add")
    @PostMapping()
    @RepeatSubmit()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DmBaseComplexSaveBo bo) {
        String code=bo.getBase().getCode();
        String str=bo.getBase().getXzqh()+""+bo.getBase().getDmlb();
        if(!code.startsWith(str)){
            return R.fail("地名编码错误，须以【"+str+"】开头。");
        }
        return toAjax(iDmService.insertByBo(bo));
    }

    /**
     * 修改地名属性信息
     */
    @SaCheckPermission("temp:dm:edit")
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DmBaseComplexSaveBo bo) {
        return toAjax(iDmService.updateByBo(bo));
    }

    /**
     * 删除地名属性信息
     *
     * @param ids submit ID
     */
    @SaCheckPermission("temp:dm:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iDmService.deleteWithValidByIds(Arrays.asList(ids),true));
    }






}
